<?php
class Paginacion extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        //cargamos la libreria tabla con paginacion
        $this->load->library('table_lib','','tabla1');
        $this->load->library('form_lib','','form1');
        $this->load->helper('url');
        $this->load->library('session');
    }
     
    public function index()
    {

        //formulario
        $this->form1->label("Titulo","","sibTexto1");
        $this->form1->label("Contenido","","sibTexto1");
        $this->form1->label("Texto","","sibTexto1");
        $this->form1->label("Número","","sibTexto1");
        $this->form1->label("Tipo","","sibTexto1");        
        //public function labelBB($label, $name, $class){
        $this->form1->input("title","","input0");
        $this->form1->input("slug","","input0");
        $this->form1->input("text","","input0");
        $this->form1->input("number","","input0");
        $this->form1->input("id_news_tipo","","input0");        
        //public function create($filas, $columnas, $ancho='100%', $arrayAncho, $classEnc='', $classImp='', $classPar='', $action){
        $this->form1->submit("guardar_noticia","Guardar Noticia");
        $arrayAncho = array('10%','90%');
        $data['form']=$this->form1->create(5,2,'50%',$arrayAncho,'','','','index.php/paginacion/registrar');

        //grid
        $array_head = array('ID', 'TITULO', 'CONTENIDO','TEXTO', 'NUMERO', 'TIPO', 'ELIMINAR', 'MODIFICAR');
        $list_fields = array('id','title','slug','text','number','tipo');
        $link_fields = array(
                     "0"=>array('index.php/paginacion/eliminar/','id','Eliminación',$this->uri->segment(3)),//class method, campo tabla, segment
                     "1"=>array('index.php/paginacion/editar/','id',"<img src=\"".base_url()."/img/detalle.png\" title=\"Modificar\"/>",$this->uri->segment(3))
                     );

        //($model, $get, $total_row, $base_url, $prefix, $per_page, $array_head, $list_fields, $link_fields, $ancho_tabla)
        $data['tabla']=$this->tabla1->crear_tabla('noticias_model', 'get_noticias', 'total_filas', 'index.php/paginacion/', '/page/', 10,
        $array_head, $list_fields, $link_fields, '100%');

        $this->load->view('paginacion/index_view.php',$data);

     }

    public function editar($id)
    {
        $this->load->model('noticias_model');
        $noticia = $this->noticias_model->get_noticia($id);


        //formulario
        $this->form1->label("Titulo","","sibTexto1");
        $this->form1->label("Contenido","","sibTexto1");
        $this->form1->label("Texto","","sibTexto1");
        $this->form1->label("Número","","sibTexto1");
        $this->form1->label("","","");
        $this->form1->label("","","");
        //public function labelBB($label, $name, $class){
        $this->form1->input("title",$noticia[0]['title'],"input0");
        $this->form1->input("slug",$noticia[0]['slug'],"input0");
        $this->form1->input("text",$noticia[0]['text'],"input0");
        $this->form1->input("number",$noticia[0]['number'],"input0");
        $this->form1->hidden("id",$noticia[0]['id'],"input0");
        $this->form1->hidden("segment",$this->uri->segment(4),"input0");
        //public function create($filas, $columnas, $ancho='100%', $arrayAncho, $classEnc='', $classImp='', $classPar='', $action){
        $this->form1->submit("actualizar_noticia","Actualizar Noticia");

        $arrayAncho = array('10%','90%');
        $data['form']=$this->form1->create(6,2,'50%',$arrayAncho,'','','','index.php/paginacion/actualizar');

        $this->load->view('paginacion/edit_view.php',$data);

     }

    public function eliminar($id,$segment='')
    {
      $this->load->model('del_model');
      $this->del_model->delete('news','id',$id);
      $this->session->set_flashdata('msg1', 'Registro Eliminado con éxito');
      if($segment==''){
          redirect("index.php/paginacion/");
      }else{
          redirect("index.php/paginacion/page/".$segment);
      }
    }

    public function registrar()
    {
      $this->load->model('insert_model');
      $adicional=array('id'=>'NULL');
      $excluir=array('guardar_noticia');
      $this->insert_model->insert('news',$adicional,$excluir);
      $this->session->set_flashdata('msg1', 'Registro Ingresado!');
      redirect("index.php/paginacion/");
    }

    public function actualizar()
    {
      $this->load->model('update_model');
      $adicional=array();
      $excluir=array('actualizar_noticia','segment');
      $this->update_model->update('news','id',$this->input->post('id'),$adicional,$excluir);
      $this->session->set_flashdata('msg1', 'Registro Actualizado!');
      if($this->input->post('segment')==''){
           redirect("index.php/paginacion/");
      }else{
           redirect("index.php/paginacion/page/".$this->input->post('segment'));
      }
    }
}